package cn.dankal.lequ.ui.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者： fred on 16/5/12 21:29.
 * 邮箱：fred_kang@dankal.cn
 */
public class TagLayout extends ViewGroup {

    List<List<View>> allViews = new ArrayList<List<View>>();
    Context context;
    /*
    整体有很多行lineViews,每行有很多个View,每行有行宽，有行高，每个View有宽度，高度，边距
    第几行的某个view位置取决于左边距，上边距
    左边距的左边的linewidth+自身的lmargin
    上边距的上边的lineHeight+自身的tmargin
     */
    //存放高度
    List<Integer> lineHeights = new ArrayList<Integer>();

    public TagLayout(Context context) {
        super(context);
        this.context = context;
    }

    public TagLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(context, attrs);
    }

    //根据childView宽高计算自己的宽高
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //测量模式和大小，测量模式也是int常量
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        int lineWidth = 0, lineHeight = 0;
        int width = 0, height = 0;

        List<View> lineViews = new ArrayList<View>();
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            //先测量再获取childView宽度,childView的宽度等于自身的宽度+左右边距
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            // 得到child的lp
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
            if (lineWidth + childWidth > sizeWidth) {
                allViews.add(lineViews);//把上一行的lineViews加到allViews
                //如果行宽加新加入的View的宽度大于有精确值的宽度，换行之后去最大宽度的
                width = Math.max(lineWidth, childWidth);// 取最大的
                lineWidth = childWidth; // 重新开启新行，开始记录
                //叠加高度
                lineHeight += childHeight;
                lineHeights.add(lineHeight);
                lineViews = new ArrayList<View>();
                lineViews.add(child);
            } else {
                //如果没超过，增加行宽
                lineWidth += childWidth;
//如果childView高度很高的话，换行后留白就很多了，不过标签的话没那么高，感觉也可以拓展到瀑布流的方式
                lineHeight = Math.max(lineHeight, childHeight);
                lineViews.add(child);
            }
            // 如果是最后一个，则将当前记录的最大宽度和当前lineWidth做比较
            if (i == getChildCount() - 1) {
                width = Math.max(width, lineWidth);
                height += lineHeight;
            }
        }
        setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth
                : width, (modeHeight == MeasureSpec.EXACTLY) ? sizeHeight
                : height);
    }

    //布局childView,确定childView的位置
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int left = 0, top = 0;
        //多少行
        for (int i = 0; i < allViews.size(); i++) {
            int lineHeight = lineHeights.get(i);
            List<View> lineViews = allViews.get(i);
            //每行多少个
            for (int j = 0; j < lineViews.size(); j++) {
                int cl, ct, cr, cb;
                View child = lineViews.get(j);
                MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
                cl = left + lp.leftMargin;
                ct = top + lp.topMargin;
                cr = cl + getMeasuredWidth();//lp.rightMargin 不需要
                cb = ct + getMeasuredHeight();
                //每个设置布局
                child.layout(cl, ct, cr, cb);
                left += lp.leftMargin + getMeasuredWidth() + lp.rightMargin;
            }
            //换行后左边距滞空
            left = 0;
            top += lineHeight;
        }
        allViews.clear();
        lineHeights.clear();
    }

}
